package com.tianaceae.educational.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.tianaceae.educational.domain.EduStageDetails;
import com.tianaceae.educational.mapper.EduStageMapper;
import com.tianaceae.educational.domain.EduStage;
import com.tianaceae.educational.service.IEduStageService;

/**
 * 教务阶段课程Service业务层处理
 * 
 * @author zhouhonggang
 * @date 2023-03-29
 */
@Service
public class EduStageServiceImpl implements IEduStageService 
{
    @Autowired
    private EduStageMapper eduStageMapper;

    /**
     * 查询教务阶段课程
     * 
     * @param id 教务阶段课程主键
     * @return 教务阶段课程
     */
    @Override
    public EduStage selectEduStageById(Long id)
    {
        return eduStageMapper.selectEduStageById(id);
    }

    /**
     * 查询教务阶段课程列表
     * 
     * @param eduStage 教务阶段课程
     * @return 教务阶段课程
     */
    @Override
    public List<EduStage> selectEduStageList(EduStage eduStage)
    {
        return eduStageMapper.selectEduStageList(eduStage);
    }

    /**
     * 新增教务阶段课程
     * 
     * @param eduStage 教务阶段课程
     * @return 结果
     */
    @Transactional
    @Override
    public int insertEduStage(EduStage eduStage)
    {
        int rows = eduStageMapper.insertEduStage(eduStage);
        insertEduStageDetails(eduStage);
        return rows;
    }

    /**
     * 修改教务阶段课程
     * 
     * @param eduStage 教务阶段课程
     * @return 结果
     */
    @Transactional
    @Override
    public int updateEduStage(EduStage eduStage)
    {
        eduStageMapper.deleteEduStageDetailsByEduStageId(eduStage.getId());
        insertEduStageDetails(eduStage);
        return eduStageMapper.updateEduStage(eduStage);
    }

    /**
     * 批量删除教务阶段课程
     * 
     * @param ids 需要删除的教务阶段课程主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteEduStageByIds(Long[] ids)
    {
        eduStageMapper.deleteEduStageDetailsByEduStageIds(ids);
        return eduStageMapper.deleteEduStageByIds(ids);
    }

    /**
     * 删除教务阶段课程信息
     * 
     * @param id 教务阶段课程主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteEduStageById(Long id)
    {
        eduStageMapper.deleteEduStageDetailsByEduStageId(id);
        return eduStageMapper.deleteEduStageById(id);
    }

    /**
     * 新增教务阶段课程详情信息
     * 
     * @param eduStage 教务阶段课程对象
     */
    public void insertEduStageDetails(EduStage eduStage)
    {
        List<EduStageDetails> eduStageDetailsList = eduStage.getEduStageDetailsList();
        Long id = eduStage.getId();
        if (StringUtils.isNotNull(eduStageDetailsList))
        {
            List<EduStageDetails> list = new ArrayList<EduStageDetails>();
            for (EduStageDetails eduStageDetails : eduStageDetailsList)
            {
                eduStageDetails.setEduStageId(id);
                list.add(eduStageDetails);
            }
            if (list.size() > 0)
            {
                eduStageMapper.batchEduStageDetails(list);
            }
        }
    }
}
